{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: librosa in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (0.10.1)\n",
      "Requirement already satisfied: audioread>=2.1.9 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (3.0.1)\n",
      "Requirement already satisfied: numpy!=1.22.0,!=1.22.1,!=1.22.2,>=1.20.3 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (1.23.5)\n",
      "Requirement already satisfied: scipy>=1.2.0 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (1.10.1)\n",
      "Requirement already satisfied: scikit-learn>=0.20.0 in c:\\users\\18745\\appdata\\roaming\\python\\python310\\site-packages (from librosa) (1.3.1)\n",
      "Requirement already satisfied: joblib>=0.14 in c:\\users\\18745\\appdata\\roaming\\python\\python310\\site-packages (from librosa) (1.3.2)\n",
      "Requirement already satisfied: decorator>=4.3.0 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (4.4.2)\n",
      "Requirement already satisfied: numba>=0.51.0 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (0.58.1)\n",
      "Requirement already satisfied: soundfile>=0.12.1 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (0.12.1)\n",
      "Requirement already satisfied: pooch>=1.0 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (1.8.0)\n",
      "Requirement already satisfied: soxr>=0.3.2 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (0.3.7)\n",
      "Requirement already satisfied: typing-extensions>=4.1.1 in c:\\users\\18745\\appdata\\roaming\\python\\python310\\site-packages (from librosa) (4.8.0)\n",
      "Requirement already satisfied: lazy-loader>=0.1 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (0.3)\n",
      "Requirement already satisfied: msgpack>=1.0 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from librosa) (1.0.7)\n",
      "Requirement already satisfied: llvmlite<0.42,>=0.41.0dev0 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from numba>=0.51.0->librosa) (0.41.1)\n",
      "Requirement already satisfied: platformdirs>=2.5.0 in c:\\users\\18745\\appdata\\roaming\\python\\python310\\site-packages (from pooch>=1.0->librosa) (3.11.0)\n",
      "Requirement already satisfied: packaging>=20.0 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from pooch>=1.0->librosa) (23.2)\n",
      "Requirement already satisfied: requests>=2.19.0 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from pooch>=1.0->librosa) (2.31.0)\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in c:\\users\\18745\\appdata\\roaming\\python\\python310\\site-packages (from scikit-learn>=0.20.0->librosa) (3.2.0)\n",
      "Requirement already satisfied: cffi>=1.0 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from soundfile>=0.12.1->librosa) (1.16.0)\n",
      "Requirement already satisfied: pycparser in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from cffi>=1.0->soundfile>=0.12.1->librosa) (2.21)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from requests>=2.19.0->pooch>=1.0->librosa) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from requests>=2.19.0->pooch>=1.0->librosa) (3.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from requests>=2.19.0->pooch>=1.0->librosa) (1.26.18)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\18745\\.conda\\envs\\py10\\lib\\site-packages (from requests>=2.19.0->pooch>=1.0->librosa) (2023.11.17)\n"
     ]
    }
   ],
   "source": [
    "!pip3 install -q librosa matplotlib scikit-learn\n",
    "!pip3 install -U librosa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install -q  tqdm matplotlib pylab"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 音频转频谱图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "matplotlib.use('Agg') # No pictures displayed\n",
    "import librosa\n",
    "import numpy as np\n",
    "import pylab\n",
    "\n",
    "'''\n",
    "Function for converting all the waves files to image files.\n",
    "'''\n",
    "\n",
    "def wavesToSpecs(audio_folder, audio_name:str, spec_path:str):\n",
    "  \n",
    "  fname = audio_name.split('.')[0]\n",
    "\n",
    "  samples, sample_rate = librosa.load(os.path.join(audio_folder, audio_name),sr=None)\n",
    "\n",
    "  fig, ax = plt.subplots(figsize=(5,5))\n",
    "  ax.set_axis_off()\n",
    "  ax.specgram(samples,Fs=2);\n",
    "  fig.savefig(os.path.join(spec_path, f'{fname}.jpg'),bbox_inches=\"tight\",pad_inches=0)\n",
    "  plt.close(fig)\n",
    "  del sample_rate, samples, fig, ax\n",
    "  \n",
    "def wavesToMelSpec(auio_folder, audio_name, spec_path):\n",
    "\n",
    "  # 加载音频文件\n",
    "  audio_path = os.path.join(auio_folder, audio_name)\n",
    "  y, sr = librosa.load(audio_path)\n",
    "\n",
    "  # 提取梅尔频谱图\n",
    "  mel_spec = librosa.feature.melspectrogram(y=y, sr=sr)\n",
    "\n",
    "  librosa.display.specshow(librosa.power_to_db(mel_spec, ref=np.max))\n",
    "  pylab.axis('off') # No axis\n",
    "  pylab.axes([0., 0., 1., 1.], frameon=False, xticks=[], yticks=[])\n",
    "  pylab.savefig(os.path.join(spec_path, audio_name.split('.')[0]+'.jpg'), bbox_inches=None, pad_inchies=0)\n",
    "  pylab.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 音频文件准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "def get_file_from_folder(folder, suffixes='.jpg'):\n",
    "    files = [f for f in os.listdir(folder) if f.endswith(suffixes)]\n",
    "    return files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "HOME = os.getcwd()\n",
    "print(HOME)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "ORIGIN_IMAGE_FOLOER = os.path.join(HOME, 'dataset/free-spoken-digit-dataset-master/recordings')\n",
    "print(ORIGIN_IMAGE_FOLOER)\n",
    "OUTPUT_FOLDER = os.path.join(HOME, 'dataset/annotationed')\n",
    "print(OUTPUT_FOLDER)\n",
    "\n",
    "try:\n",
    "    os.makedirs(OUTPUT_FOLDER, exist_ok=True)\n",
    "except Exception as e:\n",
    "    print(f\"Error creating directory: {e}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
